/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Core;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 *
 * @author LamHuynh
 */
public class CustomerDetails {

    private String fileName = "";
    XPathFactory factory;
    XPath xpath;
    InputSource inputSource;

    public CustomerDetails(String file) {
        this.fileName = file;
        this.factory = XPathFactory.newInstance();
        this.xpath = factory.newXPath();
        this.inputSource = new InputSource(fileName);
    }
    

    public ArrayList<CustomerDetail> getCustomerDetails() {
        ArrayList<CustomerDetail> cusDetails = new ArrayList<CustomerDetail>();
        try {

            XPathExpression expression = xpath.compile("/Customers/Customer");
            NodeList nodeList = (NodeList) expression.evaluate(inputSource, XPathConstants.NODESET);

            for (int i = 0, n = nodeList.getLength();i < n; i++) {
                Node currentNode = nodeList.item(i);
                int id = 0;
                String username = null;
                String fullName = null;
                String birthday = null;
                String phoneNo = null;
                String address = null;
                String email = null;
                String password = null;
                for (int j = 0; j < currentNode.getChildNodes().getLength(); j++) {
                    Node childNode = currentNode.getChildNodes().item(j);
                    if (childNode.getNodeName().equals("Id")) {
                        id = Integer.parseInt(childNode.getTextContent());
                    }
                    if (childNode.getNodeName().equals("Username")) {
                        username = childNode.getTextContent();
                    }
                    if (childNode.getNodeName().equals("FullName")) {
                        fullName = childNode.getTextContent();
                    }
                    if (childNode.getNodeName().equals("Birthday")) {
                        birthday = childNode.getTextContent();
                    }
                    if (childNode.getNodeName().equals("PhoneNo")) {
                        phoneNo = childNode.getTextContent();
                    }
                    if (childNode.getNodeName().equals("Address")) {
                        address = childNode.getTextContent();                        
                    }
                    if (childNode.getNodeName().equals("Email")) {
                        email = childNode.getTextContent();
                    }

                    if (childNode.getNodeName().equals("Password")) {
                        password = childNode.getTextContent();
                    }

                }
                cusDetails.add(new CustomerDetail(id, username, fullName, birthday, phoneNo, address, email, password));

            }
        } catch (XPathExpressionException ex) {
            Logger.getLogger(CustomerDetails.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cusDetails;
    }
}
